package com.sun.designpatternsdemo.chainofresopnsibility

import android.os.Bundle
import com.sun.designpatternsdemo.R
import com.sun.designpatternsdemo.app.AppConstant
import com.sun.designpatternsdemo.app.EMTagHandler
import com.sun.designpatternsdemo.base.BaseActivity
import com.sun.designpatternsdemo.databinding.ActivityChainOfResponsibilityBinding

class ChainOfResponsibilityActivity : BaseActivity<ActivityChainOfResponsibilityBinding>() {

    override fun layoutId(): Int =R.layout.activity_chain_of_responsibility

    override fun initView(savedInstanceState: Bundle?) {
        title = "责任链模式"
        mDataBind.apply {
            tvDefine.text = EMTagHandler.fromHtml(AppConstant.CHAIN_OF_RESPONSIBILITY_DEFINE)
            val logger = getChainOfLoggers()
            btInfo.setOnClickListener {
                logger.logMessage(AbstractLogger.INFO,"this is an information.")
            }
            btDebug.setOnClickListener {

                // ---: File::Logger  this is a debug level information.
                // ---: Standard Console::Logger  this is a debug level information.
                logger.logMessage(AbstractLogger.DEBUG, "this is a debug level information.")
            }
            btError.setOnClickListener {

                // ---: Error Console::Logger  this is a error level information.
                // ---: File::Logger  this is a error level information.
                // ---: Standard Console::Logger  this is a error level information.
                logger.logMessage(AbstractLogger.ERROR, "this is a error level information.")
            }
        }
    }

    private fun getChainOfLoggers(): AbstractLogger {
        val errorLogger = ErrorLogger(AbstractLogger.ERROR)
        val fileLogger = FileLogger(AbstractLogger.DEBUG)
        val consoleLogger = ConsoleLogger(AbstractLogger.INFO)
        errorLogger.nextLogger = fileLogger
        fileLogger.nextLogger = consoleLogger
        return errorLogger
    }
}